﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using NPOI.OpenXmlFormats.Spreadsheet;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace DFGK_WMS.Repository
{
    public class SchedulerRepository : BaseRepository<TScheduledTask>, ISchedulerRepository
    {
        public SchedulerRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 更新质量等级
        /// </summary>
        /// <returns></returns>
        public bool UpQualityState(out int count)
        {
            try
            {
                int planCount = 0;
                var stocktakingPlanList = Context.TStocktakingPlans.ToList();
                count = 0;
                foreach (var planItem in stocktakingPlanList)
                {
                    if (DateTime.Now <= planItem.BeginDate || DateTime.Now >= planItem.EndDate)
                    {
                        continue;
                    }
                    planCount++;
                    var list = Context.TInventories.ToList();
                    foreach (var item in list)
                    {
                        var datestring = item.CreateDate.ToString();
                        var date = item.CreateDate.ToString().Substring(5, 1);
                        int number = int.Parse(date);

                        var datefirst = item.CreateDate.ToString().Substring(0, 5);
                        var dateend = item.CreateDate.ToString().Substring(6);
                        int dateInt = int.Parse(date);
                        var newTime = dateInt + 3;
                        if (newTime == 13)
                        {
                            newTime = 1;
                        }
                        else if (newTime == 14)
                        {
                            newTime = 2;
                        }
                        else if (newTime == 15)
                        {
                            newTime = 3;
                        }
                        DateTime dateTime = DateTime.Parse(datefirst + newTime + dateend);

                        var inventoryList = Context.TInventories.AsQueryable();
                        DateTime now = DateTime.Now;
                        string dateTimeNow = now.Year.ToString() + "-" + now.Month.ToString() + "-" + now.Day.ToString();
                        DateTime dateTime2 = DateTime.Parse(dateTimeNow);
                        if (item.QualityState == null)
                        {
                            continue;
                        }
                        if (dateTime2 >= dateTime)
                        {
                            item.QualityState = 1;
                            Context.TInventories.Update(item);
                            count = count + 1;
                            if (Context.SaveChanges() <= 0)
                            {
                                return false;
                            }
                        }
                    }

                    return true;
                }
                if (planCount <= 0)
                {
                    count = 0;
                    return true;
                }

                return true;



            }
            catch (Exception ex)
            {
                count = 0;
                return false;
            }
        }
    }
}
